// announcements.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scroll_top:'',
    time:'2017.07.26 15:30',
    content: '',
    replyContent:'',
    focus: false,
    ava:'http://img5.imgtn.bdimg.com/it/u=3948845830,4262018369&fm=26&gp=0.jpg',
    comment:[]
  },
  getData(){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let self = this,
        baseUrl = getApp().globalData.baseUrl,
        access_token = getApp().globalData.access_token
    wx.request({
      url: baseUrl + '/Notice/index',
      method: 'get',
      data: { access_token },
      success: data => {
        console.log(data.data)
        if(data.data.code == 200){
          data.data.data[0].comment.comments = data.data.data[0].comment.comments || []
          // data.data.data[0].comment.comments.forEach( value => {
          //   value.create_time = new Date(parseInt(value.create_time)).toLocaleString()
          // })
          self.setData({
            content: data.data.data[0].content,
            comment: data.data.data[0].comment,
            time: data.data.data[0].create_time,
            notice_id: data.data.data[0].id,
            ava: data.data.data[0].user_info.portrait
          })
          wx.hideLoading()
        }
      }
    })
  },
  submit(){
    if(this.data.input_value){
      // console.log(this.data.replyTo, this.data.reply_id)
      // return
      (this.data.reply_id && this.data.replyTo) ? this.reply_submit() : this.write_comment()
    }else{
      wx.showToast({
        title: '不能提交空内容',
        image: '../../image/error.png'
      })
    }
  },
  write(){
    this.setData({
      reply_id: null,
      replyTo: null,
      focus: true
    })
  },
  // write comment
  write_comment(){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let self = this,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      content = this.data.input_value,
      notice_id = this.data.notice_id
    wx.request({
      url: baseUrl +  '/Notice/post_comment',
      method: 'post',
      header,
      data: { access_token, notice_id, content},
      success: data => {
        console.log(data.data.code)
        if (data.data.code == 200){
          wx.hideLoading()
          wx.showToast({
            title: '评论成功！',
          })
          setTimeout(function(){
            self.getData()
          },1000)
        }
      }
    })
  },
  // reply
  reply(e){

    let self = this,
      nickname = e.currentTarget.dataset.name
      console.log(nickname)
    this.setData({
      replyTo: nickname,
      reply_id: e.currentTarget.dataset.id,
      focus: true,
    })
  },
  reply_submit(){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let self = this,
      baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      header = getApp().globalData.header,
      content = this.data.input_value,
      reply_id = this.data.reply_id,
      notice_id = this.data.notice_id
    wx.request({
      url: baseUrl + '/Notice/post_reply',
      method: 'post',
      header,
      data: { access_token, notice_id, reply_id, content },
      success: data => {
        console.log(data.data)
        if(data.data.code == 200){
          wx.hideLoading()
          wx.showToast({
            title: '回复成功！',
            mask: true,
          })
          setTimeout(function(){
            self.getData()
          },1000)
        }
      }
    })

  },
  input(e){
    this.setData({
      input_value : e.detail.value
    })
  },
  clear(){
    this.setData({
      // replyTo: '',
      // // input_value:'',
      // reply_id:''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})